home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / netprog.zip / NETPROG.TAR / record.s5 / main4.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  2KB  |  62 lines

  1. /*
  2.  * Recording process, fourth try: use pseudo-terminals,
  3.  * copying the mode of the pty slave from the tty on stdin.
  4.  * We also set the mode of the tty on stdin to raw.
  5.  * Additionally, we now disassociate the child from its controlling
  6.  * terminal, so that the pty slave can become its control terminal.
  7.  */
  8.  
  9. #include    <sys/types.h>
  10. #include    <signal.h>
  11.  
  12. main(argc, argv, envp)
  13. int    argc;
  14. char    **argv;
  15. char    **envp;
  16. {
  17.     int    master_fd, slave_fd, childpid;
  18.  
  19.     if (!isatty(0) || !isatty(1))
  20.         err_quit("stdin and stdout must be a terminal");
  21.  
  22.     if ( (master_fd = pty_master()) < 0)
  23.         err_sys("can't open master pty");
  24.     if (tty_getmode(0) < 0)
  25.         err_sys("can't get tty mode of standard input");
  26.  
  27.     if ( (childpid = fork()) < 0)
  28.         err_sys("can't fork");
  29.     else if (childpid == 0) {    /* child process */
  30.         /*
  31.          * Disassociate from our control terminal so we can acquire
  32.          * the pseudo-terminal slave as our control tty.
  33.          * This call also makes us a process group leader, which is
  34.          * necessary to acquire a control tty.
  35.          */
  36.  
  37.         setpgrp();
  38.  
  39.         /*
  40.          * Now open slave pty device.
  41.          */
  42.  
  43.         if ( (slave_fd = pty_slave(master_fd)) < 0)
  44.             err_sys("can't open pty slave");
  45.         close(master_fd);
  46.         if (tty_setmode(slave_fd) < 0)
  47.             err_sys("can't set tty mode of pty slave");
  48.  
  49.         exec_shell(slave_fd, argv, envp);
  50.             /* NOTREACHED */
  51.     }
  52.  
  53.     if (tty_raw(0) < 0)            /* set stdin tty to raw mode */
  54.         err_sys("tty_raw error");
  55.  
  56.     pass_all(master_fd, childpid);
  57.  
  58.     if (tty_reset(0) < 0)            /* reset stdin mode */
  59.         err_sys("tty_reset error");
  60.     exit(0);
  61. }
  62.